package com.xsz.mybatis.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xsz.mybatis.entity.Product;
import com.xsz.mybatis.exception.SdjuException;
import com.xsz.mybatis.service.ProductService;
import com.xsz.mybatis.vo.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 产品 前端控制器
 * </p>
 *
 * @authoResultVOYgn
 * @since 2020-04-19
 */
@RestController
@RequestMapping("/mybatis/product")
public class ProductController{

    @Resource
    private ProductService productService;


    //1 查询产品表所有数据
    //rest风格
    @ApiOperation(value = "所有产品列表")
    @GetMapping("findAll")
    public R findAllProduct() {
        //调用service的方法实现查询所有的操作
        List<Product> list = productService.list(null);
        return R.ok().data("items",list);
    }

    //2 逻辑删除产品的方法
    @ApiOperation(value = "逻辑删除产品")
    @DeleteMapping("delete/{id}")
    public R removeProduct(@ApiParam(name = "id", value = "产品ID", required = true)
                           @PathVariable String id) {
        boolean flag = productService.removeById(id);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //3 分页查询产品的方法
    //current 当前页
    //limit 每页记录数
    @GetMapping("pageProduct/{current}/{limit}")
    public R pageListProduct(@PathVariable long current,
                             @PathVariable long limit) {
        //创建page对象
        Page<Product> pageProduct = new Page<>(current,limit);

//        try {
//            int i = 10/0;
//        }catch(Exception e) {
//            //执行自定义异常
//            throw new SdjuException(20001,"执行了自定义异常处理....");
//        }


        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageProduct对象里面
        productService.page(pageProduct,null);

        long total = pageProduct.getTotal();//总记录数
        List<Product> records = pageProduct.getRecords(); //数据list集合

//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);

        return R.ok().data("total",total).data("rows",records);
    }

    @GetMapping("/list/{page}/{size}")
    public R list( @PathVariable int page,@PathVariable int size)
    {
        if (page > 0 && size > 0){
            PageHelper.startPage(page, size);
        }
        List<Product> list =productService.list(null);
        PageInfo pageInfo = new PageInfo<>(list);
        return R.ok().data("pageinfo",pageInfo);
    }


    //添加产品接口的方法
    @PostMapping("addProduct")
    public R addProduct(@RequestBody Product Product) {
        boolean save = productService.save(Product);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //根据产品id进行查询
    @GetMapping("getProduct/{id}")
    public R getProduct(@PathVariable String id) {
        Product Product = productService.getById(id);
        return R.ok().data("Product",Product);
    }

    //产品修改功能
    @PostMapping("updateProduct")
    public R updateProduct(@RequestBody Product Product) {
        boolean flag = productService.updateById(Product);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

